package pers.hxy.lookweather.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qweather.sdk.bean.weather.WeatherHourlyBean;

import java.util.List;

import pers.hxy.lookweather.R;
import pers.hxy.lookweather.utils.IconUtil;

public class WeatherHourlyRecyclerViewAdapter extends RecyclerView.Adapter<WeatherHourlyRecyclerViewAdapter.ViewHolder> {
    private List<WeatherHourlyBean.HourlyBean> hourlyBeanList;

    public WeatherHourlyRecyclerViewAdapter(List<WeatherHourlyBean.HourlyBean> hourlyBeanList) {
        this.hourlyBeanList = hourlyBeanList;
    }

    public void setHourlyBeanList(List<WeatherHourlyBean.HourlyBean> hourlyBeanList) {
        this.hourlyBeanList = hourlyBeanList;
    }

    /**
     * @param parent   The ViewGroup into which the new View will be added after it is bound to
     *                 an adapter position.
     * @param viewType The view type of the new View.
     * @return
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_weather_hourly, parent, false);
        return new ViewHolder(view);
    }

    /**
     * @param holder   The ViewHolder which should be updated to represent the contents of the
     *                 item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     */
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WeatherHourlyBean.HourlyBean bean = hourlyBeanList.get(position);
        holder.tvTime.setText(bean.getFxTime().substring(11, 16));
        holder.tvText.setText(bean.getText());
        holder.ivIcon.setImageResource(IconUtil.getIconID(bean.getIcon()));
        holder.tvTemp.setText(bean.getTemp() + "℃");
        holder.tvPop.setText(bean.getPop() + "%");
    }

    /**
     * @return
     */
    @Override
    public int getItemCount() {
        return hourlyBeanList.size();
    }



    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTime;
        TextView tvText;
        ImageView ivIcon;
        TextView tvTemp;
        TextView tvPop;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTime = itemView.findViewById(R.id.tv_item_hourly_time);
            tvText = itemView.findViewById(R.id.tv_item_hourly_text);
            ivIcon = itemView.findViewById(R.id.iv_item_hourly_icon);
            tvTemp = itemView.findViewById(R.id.tv_item_hourly_temp);
            tvPop = itemView.findViewById(R.id.tv_item_hourly_pop);
        }
    }
}
